﻿
@{
    ViewBag.Title = "手动降级";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var schemas = ViewBag.Schemas as List<IOA.MES.DataAccess.VModel.VBinningSchema>;
    var workshops = schemas.GroupBy(_ => new { _.WorkshopCode, _.WorkshopName })
        .Select(_ => new KeyValuePair<string, string>(_.Key.WorkshopCode, _.Key.WorkshopName)).ToList();
}

@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <form class="layui-form layui-card-header layuiadmin-card-header-auto" method="post" action="/Report/ExportComponents">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.NewSchema">新分档规则：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <select id="selSchemas" lay-filter="selSchemas">
                                <option value="0">选择分档规则</option>
                                @foreach (var item in schemas.Where(_ => string.Equals(_.WorkshopCode, workshops.FirstOrDefault().Key, StringComparison.CurrentCultureIgnoreCase)).ToList())
                                {
                                    <option value="@item.PkId">@item.SchemaName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list btnImportBarCodes" type="button">
                            <i class="layui-icon layui-icon-upload-drag layuiadmin-button-btn"></i>导入条码
                        </button>
                        <button class="layui-btn layui-btn-primary layuiadmin-btn-list btnClearBarCodes" type="button">
                            <i class="layui-icon layui-icon-cols layuiadmin-button-btn"></i>清空条码
                        </button>
                        <button class="layui-btn layui-btn-danger layuiadmin-btn-list btnSubmit" type="button">
                            <i class="layui-icon layui-icon-senior layuiadmin-button-btn"></i>执行降级
                        </button>
                    </div>
                </div>
            </form>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var schemas = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(schemas));
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form;
                var curSchema = null,
                    data = new Array();

                form.on("select(selWorkshops)", function (data) {
                    var curSchemas = schemas.filter(function (e) {
                        return e.WorkshopCode == data.value;
                    });
                    var html = ["<option value='0'>请选择</option>"];
                    for (var i = 0; i < curSchemas.length; i++) {
                        html.push("<option value='" + curSchemas[i].PkId + "'>" + curSchemas[i].SchemaName + "</option>");
                    }
                    $("#selSchemas").html(html.join(""));
                    form.render("select");
                    curSchema = null;
                    renderData();
                });
                form.on("select(selSchemas)", function (data) {
                    curSchema = schemas.find(function (e) {
                        return e.PkId == parseInt(data.value);
                    });
                    curSchema = curSchema == undefined ? null : curSchema;
                    renderData();
                });

                var options = {
                    elem: '#table'
                    , page: false
                    , limit: 1000000
                    , data: data
                    , height: 'full-160'
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                        , { field: 'OrderNo', title: '订单号', width: 150, fixed: "left" }
                        , { field: 'WorkOrderNo', title: '工单号', width: 180, fixed: "left" }
                        , { field: 'BarCode', title: '组件条码', width: 200, fixed: "left" }
                        , { field: 'OldPower', title: '原功率', width: 80 }
                        , { field: 'OldNameplateModel', title: '原铭牌', minWidth: 180 }
                        , { field: 'NewPower', title: '新功率(预览)', width: 120 }
                        , { field: 'NewNameplateModel', title: '新铭牌(预览)', width: 180 }
                        , { field: 'Isc', title: 'Isc', minWidth: 100 }
                        , { field: 'Voc', title: 'Voc', minWidth: 100 }
                        , { field: 'Vm', title: 'Vm', minWidth: 100 }
                        , { field: 'Im', title: 'Im', minWidth: 100 }
                        , { field: 'FF', title: 'FF', minWidth: 100 }
                        , { field: 'Pmax', title: 'Pmax', minWidth: 100 }
                    ]]
                };
                table.render(options);

                $(".btnClearBarCodes").click(function () {
                    data = new Array();
                    renderData();
                });

                $(".btnImportBarCodes").click(function () {
                    layer.prompt({
                        formType: 2,
                        title: '请输入条码（每行一个）',
                        maxlength: 100000,
                        area: ['800px', '350px'] //自定义文本域宽高
                    }, function (value, index, elem) {
                        layer.close(index);
                        var barCodes = value.trim() == "" ? [] : value.trim().split('\n');
                        if (barCodes.length == 0) {
                            layui.layer.msg("请至少上传一个条码", { icon: 5 });
                            return;
                        }
                        $.ajax({
                            url: "/Quality/UploadDowngradeCodes",
                            type: "POST",
                            data: {
                                barCodes: barCodes
                            },
                            success: function (res) {
                                if (res.ErrCode != 200) {
                                    layui.layer.msg(res.ErrMsg, { icon: 5 });
                                    return;
                                }
                                data = res.Data;
                                renderData();
                            }
                        });
                    });
                });

                function renderData() {
                    options.data = data;
                    for (var i = 0; i < data.length; i++) {
                        if (curSchema == null) {
                            data[i].NewPower = 0;
                            data[i].NewNameplateModel = "";
                        } else {
                            var power = curSchema.BinningPowers.find((e) => {
                                return data[i].Pmax >= e.PowerMin && data[i].Pmax < e.PowerMax
                            });
                            if (power == null) {
                                data[i].NewPower = 0;
                                data[i].NewNameplateModel = "";
                            } else {
                                data[i].NewPower = power.Power;
                                data[i].NewNameplateModel = power.NameplateModel;
                            }
                        }
                    }

                    table.reload("table", options);
                }

                $(".btnSubmit").click(function () {
                    var barCodes = data.map((e) => {
                        return e.BarCode;
                    });
                    if (barCodes.length <= 0) {
                        layui.layer.msg("请至少降级一个组件！", { icon: 5 });
                        return;
                    } else if (curSchema == null) {
                        layui.layer.msg("请选择分档规则！", { icon: 5 });
                        return;
                    } else if (data.filter((e) => { return e.NewNameplateModel == "" }).length > 0) {
                        layui.layer.msg("条码中存在无法分档的组件！", { icon: 5 });
                        return;
                    }

                    var index = layer.load();
                    $.ajax({
                        url: "/Quality/DoManuallyDowngrade",
                        type: "POST",
                        data: {
                            barCodes: barCodes,
                            schemaId: curSchema.PkId
                        },
                        success: function (res) {
                            layer.close(index);
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layui.layer.msg("降级成功！", { icon: 6 });
                            data = [];
                            renderData();
                        }
                    });
                });
            });
        }

    </script>
}
